package com.learn.mvc.handler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 第一步：定义servlet
// 继承HttpServlet类，使用extends。
// 导入父类中的方法，使用ctrl + o快捷键
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        // 直接返回数据
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 使用 HttpServletResponse 输出数据，返回给前端
        // 在这里设置编码格式，或者在application.properties中设置
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("在myServlet中执行的servlet");
        writer.flush();
        writer.close();
    }
}
